Ext.regController("CelebController", {
	celebAnim: [],
	timeout: 0,
	mode: 0,
	"initCeleb": function(opt) {
		this.celebAnim = new CelebAnim(opt.timeout);
		this.timeout = opt.timeout;
		this.mode = opt.mode;
		Ext.dispatch({
			controller: this,
			action: "show",
			elCeleb: opt.elCeleb,
			elCritical: opt.elCritical
		});
	},
	"show": function(opt) {
		opt.elCeleb.removeCls("brick");
		opt.elCritical.removeCls("brick");
		opt.elCeleb.setStyle({
			"background-image": "url(rsc/celeb.png)"
		});
		var CelebPos = {
			top: Math.abs(Math.floor(Math.random()*($STAGE.height-$STAGE.margin)) - $CELEB.height) + $STAGE.margin,
			left: Math.abs(Math.floor(Math.random()*($STAGE.width-$STAGE.margin)) - $CELEB.width) + $STAGE.margin,
			devTop: (Math.ceil(Math.random() - 0.5) - Math.floor(Math.random() - 0.5) - 1)*$CELEB.move_dev,
			devLeft: (Math.ceil(Math.random() - 0.5) - Math.floor(Math.random() - 0.5) - 1)*$CELEB.move_dev
		}
		opt.elCeleb.setTopLeft(CelebPos.top, CelebPos.left);
		opt.elCritical.setTopLeft(CelebPos.top + $CRITICAL.top_dev, CelebPos.left + $CRITICAL.left_dev);
		if (this.mode == 1)
			this.celebAnim.addEffect(CelebPos.top, CelebPos.left, CelebPos.top + CelebPos.devTop, CelebPos.left + CelebPos.devLeft);
		Ext.dispatch({
			controller: this,
			action: "runEffect",
			elCeleb: opt.elCeleb,
			elCritical: opt.elCritical
		})
	},
	"runEffect": function(opt) {
		opt.elCeleb.show();
		this.celebAnim.getAnim()[0].run(opt.elCeleb);
		Ext.defer(function() {
			this.celebAnim.getAnim()[1].run(opt.elCeleb);
		}, this.timeout/2, this);
		Ext.defer(function() {
			opt.elCeleb.hide();
			opt.elCritical.hide();
			opt.elCeleb.setTopLeft(opt.elCeleb.getTop(), opt.elCeleb.getTop());
			var currentTime = new Date();
			if (currentTime - startTime <= $TIME_MILESTONE.max_game_time)
				Ext.dispatch({
					controller: CeleBrick.controllers.celebController,
					action: "show",
					elCeleb: opt.elCeleb,
					elCritical: opt.elCritical
				});
			else
				Ext.dispatch({
					controller: CeleBrick.controllers.mainController,
					action: "levelFinish"
				});
		}, this.timeout, this);
	}
});

CeleBrick.controllers.celebController = Ext.ControllerManager.get("CelebController");